/*
 * $RCSfile: RoomLoad.java,v $
 * $Revision: 1.1  $
 * $Date: 2012-8-10  $
 *
 * Copyright (C) 2005 Bettem, Inc. All rights reserved.
 *
 * This software is the proprietary information of Bettem, Inc.
 * Use is subject to license terms.
 */
package com.dzpk.gameserver.module.main;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import com.i366.cache.Cache;
import com.i366.mian.platformService;
import com.i366.room.model.LeveRoom;
import com.i366.room.model.SecondRoom;
import com.i366.room.model.StairRoom;
import com.i366.room.model.impl.Room;
import com.work.comm.LoadProperties;

/**
 * ��ʱ��ȡ���������ļ�
 * <p>Title: RoomLoad</p> 
 * <p>Description: </p> 
 * <p>Copyright: Copyright (c) 2006</p> 
 * @author shenxing.ruan
 * @version 1.0
 */
public class RoomLoad implements Runnable{
	private int r_ = 0;
	public static Logger  logger = Logger.getLogger(RoomLoad.class);
	private final static long SPACE_LOAD_TIME = Long.valueOf(Cache.p.getProperty("load.room.space.time"));
	private final static String PATH =  "room.xml";
	private static int isLoad = -999;
	public void run() {
		while (r_ == 0) {
			try {
				loadRoomXML();
				//��ͣ
				Thread.sleep(SPACE_LOAD_TIME);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void loadRoomXML() {
		logger.debug("begin load room xml........");
        try{
            SAXBuilder saxBuilder = new SAXBuilder(LoadProperties.getProperty("wg.SAXParser"));
            Document document = saxBuilder.build(new File(PATH));
            Element root = document.getRootElement();
            //��Ӧitem�ظ������ò����ڼ���
            Map<Integer, Integer> roomItme = new HashMap<Integer,Integer>();
            int i = 1; //����һ������ 1~5
            int j = 1; //1~2
            int k = 1; //
            Map<Integer, StairRoom> srMap = Cache.getStairRoomMap();
        	// type1 ~ type5
        	if (isLoad < 0) {
        		isLoad = 999;
        		String ip = Cache.p.getProperty("http.ip");
        		int port = Integer.valueOf(Cache.p.getProperty("socket.port"));
                for (Element element : (List<Element>)root.getChildren()) {
                	StairRoom sr = new StairRoom();
                	sr.setIp(ip);
                	sr.setPort(port);
                	srMap.put(i, sr);
                	j = 1;
                	Map<Integer, SecondRoom> secondRoomMap = sr.getSecondRoom();
                	for (Element element2 : (List<Element>)element.getChildren()) {
                		SecondRoom secondRoom = new SecondRoom();
                		int wait = Integer.valueOf(element2.getAttributeValue("wait"));
                		secondRoom.setWint(wait);
                		secondRoomMap.put(j, secondRoom);
                		k = 1;
                		Map<Integer, LeveRoom> leveMap = secondRoom.getLeveRoomMap();
                		for (Element element3 : (List<Element>)element2.getChildren()) {
                			//id="1001000" manzhu="1" damanzhu="2" gold="20"   count="30"
                			int id = Integer.valueOf(element3.getAttributeValue("id"));
                			int manzhu = Integer.valueOf(element3.getAttributeValue("manzhu"));
                			int damanzhu = Integer.valueOf(element3.getAttributeValue("damanzhu"));
                			int gold = Integer.valueOf(element3.getAttributeValue("gold"));
                			int count = Integer.valueOf(element3.getAttributeValue("count"));
                			int fuwufei = Integer.valueOf(element3.getAttributeValue("fuwufei"));
                			//������Я���� ������
                			int maxgold = Integer.valueOf(element3.getAttributeValue("maxgold"));
                			int min = Integer.valueOf(element3.getAttributeValue("min"));
                			int max = Integer.valueOf(element3.getAttributeValue("max"));
                			double sl = Double.valueOf(element3.getAttributeValue("sl"));

                			
                			if (count < 1) {
                				continue;
                			}
                			LeveRoom leveRoom = new LeveRoom();
                			leveRoom.setId(id);
                			leveRoom.setManzhu(manzhu);
                			leveRoom.setDamanzhu(damanzhu);
                			leveRoom.setGold(gold);
                			leveRoom.setCount(count);
                			leveRoom.setSl(sl);
                			leveRoom.setRoomType( i == 5 ? 2 : 1);// 1��Ϸ�� 2����
                			if (i == 5) {
                    			int d1 = Integer.valueOf(element3.getAttributeValue("d1"));
                    			int d2 = Integer.valueOf(element3.getAttributeValue("d2"));
                				leveRoom.setD1(d1);
                				leveRoom.setD2(d2);
                			}
                			leveRoom.setInitSize(0);
                			leveRoom.setRoomPaht(i * 10 + j);
                			leveRoom.setWait(wait);
                			leveRoom.setFuwuqfei(fuwufei);
                			leveRoom.setMaxgold(maxgold);
                			leveRoom.setMin(min);
                			leveRoom.setMax(max);
                			
                			leveMap.put(leveRoom.getId(), leveRoom);
                			k ++ ;
                			Map<Integer, Room> RoomMap = leveRoom.getRoomMap();
                			for (int m = 1 ; m < leveRoom.getCount() + 1 ; m ++) {
                				//�ſն��󷿼�
                				RoomMap.put(leveRoom.getId() + m, null);
                			}
                		}
                		j ++;
                	}
                	i ++;
                }
        	}else {
                for (Element element : (List<Element>)root.getChildren()) {
                	StairRoom sr = srMap.get(i);
                	j = 1;
                	Map<Integer, SecondRoom> secondRoomMap = sr.getSecondRoom();
                	for (Element element2 : (List<Element>)element.getChildren()) {
                		SecondRoom secondRoom = secondRoomMap.get(j);
                		int wait = Integer.valueOf(element2.getAttributeValue("wait"));
                		secondRoom.setWint(wait);

                		k = 1;
                		Map<Integer, LeveRoom> leveMap = secondRoom.getLeveRoomMap();
                		for (Element element3 : (List<Element>)element2.getChildren()) {
                			//id="1001000" manzhu="1" damanzhu="2" gold="20"   count="30"
                			int id = Integer.valueOf(element3.getAttributeValue("id"));
                			int manzhu = Integer.valueOf(element3.getAttributeValue("manzhu"));
                			int damanzhu = Integer.valueOf(element3.getAttributeValue("damanzhu"));
                			int gold = Integer.valueOf(element3.getAttributeValue("gold"));
                			int count = Integer.valueOf(element3.getAttributeValue("count"));
                			int fuwufei = Integer.valueOf(element3.getAttributeValue("fuwufei"));
                			//������Я���� ������
                			int maxgold = Integer.valueOf(element3.getAttributeValue("maxgold"));
                			int min = Integer.valueOf(element3.getAttributeValue("min"));
                			int max = Integer.valueOf(element3.getAttributeValue("max"));
                			double sl = Double.valueOf(element3.getAttributeValue("sl"));
                			
                			LeveRoom leveRoom = leveMap.get(id);
                			if (count == 0 && leveRoom == null) {
                				continue;
                			}
                			if (leveRoom == null) {
                				leveRoom = new LeveRoom();
                				Map<Integer, Room> roomMap = leveRoom.getRoomMap();
                    			leveRoom.setId(id);
                    			leveRoom.setManzhu(manzhu);
                    			leveRoom.setDamanzhu(damanzhu);
                    			leveRoom.setGold(gold);
                    			leveRoom.setCount(count);
                    			leveRoom.setSl(sl);
                    			leveRoom.setRoomType( i == 5 ? 2 : 1);// 1��Ϸ�� 2����
                    			if (i == 5) {
                        			int d1 = Integer.valueOf(element3.getAttributeValue("d1"));
                        			int d2 = Integer.valueOf(element3.getAttributeValue("d2"));
                    				leveRoom.setD1(d1);
                    				leveRoom.setD2(d2);
                    			}
                    			leveRoom.setInitSize(0);
                    			leveRoom.setRoomPaht(i * 10 + j);
                    			leveRoom.setWait(wait);
                    			leveRoom.setFuwuqfei(fuwufei);
                    			leveRoom.setMaxgold(maxgold);
                    			leveRoom.setMin(min);
                    			leveRoom.setMax(max);
                    			leveMap.put(id, leveRoom);
                    			for (int l = 1 ; l < leveRoom.getCount() + 1 ; l ++) {
                    				//�ſն��󷿼�
                    				roomMap.put(leveRoom.getId() + l, null);
                    			}
                			}else {
                				Map<Integer, Room> roomMap = leveRoom.getRoomMap();
                				//��Ҫ���ӷ�������
                				if (count - leveRoom.getCount() > 0) {
                					for (int m = leveRoom.getCount() + 1 ; m < count + 1 ; m ++) {
                						roomMap.put(leveRoom.getId() + m, null);
                					}
                				//���ٷ�����
                				}else if (count - leveRoom.getCount() < 0) {
                					for (int m = count ; m < leveRoom.getCount() + 1 ; m ++) {
                						Room room = roomMap.get(leveRoom.getId() + m);
                						if (room == null) {
                							roomMap.remove(leveRoom.getId() + m);
                						}
                					}
                				}
                				leveRoom.setCount(count);
                			}
                			k ++ ;
                		}
                		j ++;
                	}
                	i ++;
                }
        	}
        	logger.debug("load room.xml ok........");
        }catch(JDOMException ex){
            logger.error("parse class mapping error", ex);
        }catch(IOException ex){
            logger.error("xml config read error", ex);
        }
	}
	
	
	public void close() {
		r_ = -999;
	}
	
	public static void main(String[] args) {
		(new RoomLoad()).loadRoomXML();
	}
}

